package org.front.client.widget.listetransactionwidget;

import java.util.ArrayList;
import java.util.List;

import org.front.client.service.DistributeurService;
import org.front.client.service.DistributeurServiceAsync;
import org.front.client.widget.listetransactionwidget.event.ListeTransactionWidgetEvent;
import org.front.client.widget.listetransactionwidget.event.ListeTransactionWidgetHandler;
import org.front.client.widget.mainwidget.i18n.images.MainWidgetImages;
import org.front.shared.data.Distributeur;
import org.front.shared.data.Transaction;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ListeTransactionWidget extends Composite implements ListeTransactionWidgetHandler {

	private final DistributeurServiceAsync distributeurService = GWT.create(DistributeurService.class);

	Grid gridTransactions;

	Distributeur distributeur;

	/** UIBinder bindings */
	@UiField
	FlowPanel rootPanel;
	@UiField
	FlowPanel loading;
	@UiField
	FlowPanel panelTransactions;
	
	/** event bus on which to commit */
	private final EventBus eventBus;
	
	/** Handlers the widget registers */
	private final List<HandlerRegistration> handlersRegistration;

	/**
	 * Client Bundle resources for this widget
	 */
	public interface Resources extends ClientBundle {

		/** Instance */
		public Resources INSTANCE = GWT.create(Resources.class);

		/**
		 * @return Css
		 */
		@Source("ListeTransactionWidget.css")
		ListeTransactionWidgetCssResources css();

		/**
		 * @return Images du widget
		 */
		MainWidgetImages images();
	}

	/**
	 * Declare UIBinder for this widget
	 */
	@UiTemplate("ListeTransactionWidget.ui.xml")
	interface MainWidgetUiBinder extends UiBinder<Widget, ListeTransactionWidget> {
	}

	/** UIBinder for building interface */
	private final MainWidgetUiBinder uiBinder;

	public ListeTransactionWidget(EventBus eventBus) {
		
		this.eventBus = eventBus;

		// registration of handlers
		this.handlersRegistration = new ArrayList<HandlerRegistration>();
		
		// create UI
		this.uiBinder = GWT.create(MainWidgetUiBinder.class);
		initWidget(this.uiBinder.createAndBindUi(this));
		Resources.INSTANCE.css().ensureInjected();
		rootPanel.setStyleName("main");
	}

	public void initialiserData() {
		this.loading.setVisible(true);
		initDistributeur();
	}

	private void afficherListTransactions() {

		if (gridTransactions != null) {
			panelTransactions.remove(gridTransactions);
		}

		gridTransactions = new Grid(distributeur.getListTransactions().size(), 3);
		DateTimeFormat fmt = DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss");

		for (int i = 0; i < distributeur.getListTransactions().size(); i++) {
			Transaction transaction = distributeur.getListTransactions().get(i);
			gridTransactions.setWidget(i, 0, new Label(fmt.format(transaction.getDate())));
			gridTransactions.setWidget(i, 1, new Label(transaction.getProduit().getDescription()));
			gridTransactions.setWidget(i, 2, new Label(String.valueOf(transaction.getProduit().getPrix())));
		}

		this.loading.setVisible(false);
		panelTransactions.add(gridTransactions);
	}

	/**
	 * 
	 */
	private void initDistributeur() {
		distributeurService.getDistributeur(new AsyncCallback<Distributeur>() {

			@Override
			public void onSuccess(Distributeur result) {
				GWT.log("Success : getDistributeur");
				populateData(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Failure : getDistributeur " + caught);
			}
		});
	}

	/**
	 * Recuperation du distributeur et appel a l'affichage des blocs
	 * @param result
	 */
	private void populateData(Distributeur result) {
		distributeur = result;
		afficherListTransactions();
	}
	
	@Override
	protected void onLoad() {		
		super.onLoad();
		bind();
	}
	
	@Override
	protected void onUnload() {		
		super.onUnload();
		unbind();
	}
	
	/**
	 * Bind handler
	 */
	protected void bind() {
		this.handlersRegistration.add(this.eventBus.addHandler(ListeTransactionWidgetEvent.TYPE, this));
	}

	/**
	 * Unbing handler
	 */
	protected void unbind() {
		// remove handlers registration
		for (final HandlerRegistration hr : this.handlersRegistration) {
			hr.removeHandler();
		}
		this.handlersRegistration.clear();
	}

	@Override
	public void onMainWidget(ListeTransactionWidgetEvent event) {
		initialiserData();		
	}
}
